set matsize 11000 

* ) Insert directory
cd ""
use "Author-paper-referee level data", clear

***************************************
global authorcontrols i.editor_id female gender_missing published_papers published_from_5_no_pp USNEWS_1-USNEWS_5 is_nber ///
	employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 NBER_AG-NBER_PE degree_1 degree_2

sum $authorcontrols

* ) Top-coding distance and include missing
replace referee_distance = 6 if referee_distance>6 | referee_distance==.
replace editor_distance = 6 if editor_distance>6 | editor_distance==.

*) When doing editor analysis, don't double count observations due to referee assignment
bysort ms_number_id editor_id author_id: gen indicate = 1 if _n==1

* ) For model with "number of direct connections between author and reviewer/editor"
gen referee_one_degree = (referee_distance==1)
gen editor_one_degree = (editor_distance==1)
gen referee_Nconnections = referee_one_degree + referee_phdmatch + referee_employmentmatch + referee_isnbermatch_program 
gen editor_Nconnections = editor_one_degree + editor_phdmatch + editor_employmentmatch + editor_isnbermatch_program 
drop referee_one_degree editor_one_degree
*Generating indicators for each category
tab referee_Nconnections, generate(referee_Nconnections_raw) m
tab editor_Nconnections, generate(editor_Nconnections_raw) m
*Top code 2+ connections
replace referee_Nconnections_raw3 = referee_Nconnections_raw3+referee_Nconnections_raw4
replace editor_Nconnections_raw3 = editor_Nconnections_raw3+editor_Nconnections_raw4+editor_Nconnections_raw5

*****************************************
*****************************************
*****************************************
*PhD Match
eststo clear

*PhD Institution
eststo: logit RR referee_phdmatch i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace
*Same Employment
eststo: logit RR referee_employmentmatch i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace
*NBER programs
eststo: logit RR referee_isnbermatch_program i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace
*Degrees of separation
eststo: logit RR referee_distance_raw1-referee_distance_raw3 i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace
*Everything together
eststo: logit RR referee_phdmatch referee_employmentmatch referee_isnbermatch_program referee_distance_raw1-referee_distance_raw3 i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace
*Number of connections
eststo: logit RR referee_Nconnections_raw2-referee_Nconnections_raw3 i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace

label var RR "\underline{Outcome: Positive evaluation}"
label var referee_phdmatch "\ \ Exact same PhD institution "
label var referee_employmentmatch "\ \ Former/current colleagues"
label var referee_isnbermatch_program "\ \ Same NBER program(s)"
label var referee_distance_raw1 "\ \ Degrees of separation: 1"
label var referee_distance_raw2 "\ \ Degrees of separation: 2"
label var referee_distance_raw3 "\ \ Degrees of separation: 3"
label var referee_distance_raw4 "\ \ Degrees of separation: 4"
label var referee_distance_raw5 "\ \ Degrees of separation: 5"
label var referee_distance_raw6 "\ \ Degrees of separation: 6+"
label var referee_Nconnections_raw2 "\hline \# of direct matches: \\ \ \ - One match"
label var referee_Nconnections_raw3 "\ \ - Two+ matches"

esttab using "referee_clubmatch_logit.tex", ///
    replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) noobs nonum nonotes nogaps ///
	s(N refFE paperFE authorcontrols , label("Author-reviewer-papers" "Reviewer FE" "Editor FE" "Author controls") fmt(%12.0f)) ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)" "(9)" "(10)" "(11)" "(12)" "(13)" "(14)" "(15)") label ///
	keep(referee_phdmatch referee_employmentmatch referee_isnbermatch_program referee_distance* referee_Nconnections_raw2 referee_Nconnections_raw3)

**********************************
**********************************
**********************************
*Generate mismatch variables
gen editor_groupUSNEWSrankmismatch1 = (editor_groupUSNEWSrankmatch==0 & rank>editor_rank)
gen editor_groupUSNEWSrankmismatch2 = (editor_groupUSNEWSrankmatch==0 & rank<editor_rank)
gen referee_groupUSNEWSrankmismatch1 = (referee_groupUSNEWSrankmatch==0 & rank>referee_rank)
gen referee_groupUSNEWSrankmismatch2 = (referee_groupUSNEWSrankmatch==0 & rank<referee_rank)

*Generate mismatch variables
gen editor_groupemploymentrankmis1 = (editor_groupemploymentrankmatch==0 & IDEASemploymentrank>editor_IDEASemploymentrank)
gen editor_groupemploymentrankmis2 = (editor_groupemploymentrankmatch==0 & IDEASemploymentrank<editor_IDEASemploymentrank)
gen referee_groupemploymentrankmis1 = (referee_groupemploymentrankmatch==0 & IDEASemploymentrank>referee_IDEASemploymentrank)
gen referee_groupemploymentrankmis2 = (referee_groupemploymentrankmatch==0 & IDEASemploymentrank<referee_IDEASemploymentrank)

*T5, PhD rank and employment ranks
eststo clear
*T5 match
eststo: logit RR referee_grouptop5match i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace

*Top school match
eststo: logit RR referee_groupUSNEWSrankmatch i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace
eststo: logit RR referee_groupUSNEWSrankmismatch* i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refcontrols " ",replace
estadd local papercontrols " ",replace
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorFE " ", replace
estadd local networkcontrols "X",replace
estadd local authorcontrols "X",replace

*Employment rank match
eststo: logit RR referee_groupemploymentrankmatch i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorcontrols "X",replace
eststo: logit RR referee_groupemploymentrankmis* i.referee_id $authorcontrols [pweight=hello], vce(cluster ms_number_id)
estadd local refcontrols " ",replace
estadd local papercontrols " ",replace
estadd local refFE "X",replace
estadd local paperFE "X",replace
estadd local authorFE " ", replace
estadd local networkcontrols "X",replace
estadd local authorcontrols "X",replace

label var referee_grouptop5match "\ \ Author-reviewer both T5"
label var referee_groupUSNEWSrankmatch "\ \ Both from similar rank PhD (US News)"
label var referee_groupemploymentrankmatch "\ \ Both employed at similar rank department (IDEAS)"
label var referee_groupUSNEWSrankmismatch1 "\ \ \ \ -Not similar rank (reviewer higher)"
label var referee_groupUSNEWSrankmismatch2 "\ \ \ \ -Not similar rank (reviewer lower)"
label var referee_groupemploymentrankmis1 "\ \ \ \ -Not similar rank (reviewer higher)"
label var referee_groupemploymentrankmis2 "\ \ \ \ -Not similar rank (reviewer lower)"

esttab using "referee_signalmatch_logit.tex", ///
    replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) noobs nonum nonotes nogaps ///
	s(N refFE paperFE authorcontrols, label("Author-reviewer-papers" "Reviewer FE" "Editor FE" "Author controls") fmt(%12.0f)) ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" ) label ///
	keep(referee_grouptop5match referee_groupUSNEWSrankmatch referee_groupemploymentrankmatch referee_groupUSNEWSrankmismatch* referee_groupemploymentrankmis*)
